package yxy.game.pm2.module.yiling;

import yxy.apple.logger.Logger;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.area.AreaGroupBean;
import yxy.game.pm2.bean.module.rank.AreaGroupRankModule;
import yxy.game.pm2.bean.module.rank.PlayerRankModule;

public class RankYiLingBattle extends AreaGroupRankModule {
    protected static Logger logger = Logger.create(RankYiLingBattle.class);

    public RankYiLingBattle(AreaGroupBean areaGroupBean) {
        super(areaGroupBean);
        initInfo();
    }

    @Override
    public String rankName() {
        return PlayerRankModule.getAllRankNames().get(16);
    }

    @Override
    protected long valuePlastic(long value) {
        return value * 100;
    }

    @Override
    protected long valueRestore(long score) {
        return score / 100;
    }

    static public RankYiLingBattle getRank(String playerCode) {
        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
        AreaGroupBean areaGroupBean = AreaGroupBean.get(areaBean.getGroupId());
        return new RankYiLingBattle(areaGroupBean);
    }

    @Override
    protected String rankKey() {
        return String.format("YILING:AREAGROUP:%s:%s", getAreaGroupId(), rankName());
    }

    @Override
    public int topSize() {
        return 50;
    }

    //合战区的排行榜处理
    public void mergeAreaGroup() {
        AreaGroupBean areaGroupBean = AreaGroupBean.get(getAreaGroupId());
        try {
            logger.info("夷陵之战 BEGIN，战区：{} ----------------", areaGroupBean.getGroupId());
            RankYiLingBattle rankYiLingBattle = new RankYiLingBattle(areaGroupBean);
            all().forEach(rankEntry -> {
                try {
                    String playerCode = rankEntry.getKey();
                    PlayerBean playerBean = PlayerBean.get(playerCode); //信息
                    if (playerBean == null) {
                        logger.error("playerBean 不存在：{}", playerCode);
                        //从当前排行榜移除
                        rankYiLingBattle.remove(playerCode);
                    } else {
                        int serverId = playerBean.getServerId(); //所在服
                        ServerBean serverBean = ServerBean.get(serverId);
                        AreaBean areaBean = AreaBean.get(serverBean.getAreaId());
                        if (areaBean.getGroupId() != areaGroupBean.getGroupId()) { //所在战区 跟 排行榜所属战区不一致
                            logger.info("ERR：当前战区：{}，联盟所在战区：{}", areaGroupBean.getGroupId(), areaBean.getGroupId());

                            //从当前排行榜移除
                            rankYiLingBattle.remove(playerCode);

                            //放到新的排行榜里
                            AreaGroupBean areaGroupBeanNew = AreaGroupBean.get(areaBean.getGroupId());
                            RankYiLingBattle rankYiLingBattleNew = new RankYiLingBattle(areaGroupBeanNew);
                            rankYiLingBattleNew.put(rankEntry.getKey(), rankEntry.getValue());
                        }
                    }
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    // e.printStackTrace();
                }
            });
            logger.info("夷陵之战 END，战区：{} ----------------", areaGroupBean.getGroupId());
        } catch (Exception e) {
            logger.error("夷陵之战 ERR:{},{}", areaGroupBean.getGroupId(), e.getMessage());
        }
    }
}
